import { isArray, isObject } from './type'

/**
 *
 * @param {Function} fn
 * @param {Object} obj
 * @param  {...any} args
 */
export function apply(fn, obj, arr) {
  if (typeof fn !== 'function') {
    throw new TypeError('The first argument required to be a function')
  }
  obj = isObject(obj) ? obj : Object(obj)

  obj.tempFn = fn
  let result

  if (!arr) {
    result = obj.tempFn()
  } else if (!isArray(arr)) {
    throw new TypeError('The third argument required to be an array')
  } else {
    result = obj.tempFn(...arr)
  }
  delete obj.tempFn

  return result
}
